package cs.upt.awp.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import cs.upt.awp.data.TravelRecord;
import cs.upt.awp.data.UserRole;

public class UserRolesDAO implements DataDAO<UserRole> {

	@Autowired
	SessionFactory sessionFactory;
	
	Session session = null;
	Transaction tx = null;
	@Override
	public boolean addEntity(UserRole entity) throws Exception {
		session = sessionFactory.openSession();
		tx = session.beginTransaction();
		session.save(entity);
		tx.commit();
		session.close();
		return false;
	}

	@Override
	public UserRole getEntityById(long id) throws Exception {
		session = sessionFactory.openSession();
		UserRole role = (UserRole) session.load(
				UserRole.class, new Long(id));
		tx = session.getTransaction();
		session.beginTransaction();
		tx.commit();
		return role;
	}

	@Override
	public List<UserRole> getEntityList() throws Exception {
		session = sessionFactory.openSession();
		tx = session.beginTransaction();
		List<UserRole> roles = session.createCriteria(UserRole.class).list();
		tx.commit();
		session.close();
		return roles;
	}

	@Override
	public boolean deleteEntity(long id) throws Exception {
		session = sessionFactory.openSession();
		Object o = session.load(TravelRecord.class, id);
		tx = session.getTransaction();
		session.beginTransaction();
		session.delete(o);
		tx.commit();
		return false;
	}

}
